import { get, post } from '@/utils/request';
// 登录
const doLogin = async (code: string) => {
    try {
        const res = await post<any>("/user/login", { code: code })
        console.log("登录返回结果: ", res);
        // 保存token
        uni.setStorageSync("token", res.data);
        return res;
    } catch (error) {
        console.error("登录操作出现问题: ", error);
    }
}

// 获取邮箱验证码
const getVertifyCode = async (email: string) => {
    try {
        return await get<any>(`/user/email/vertifyCode?email=${email}`)
    } catch (error) {
        console.error("邮箱验证码出现问题: ", error);
    }
}

// 邮箱校验
interface EmailBandingDto{
    email:string;
    code:string;
}
const emailBinding = async (bandingDto: EmailBandingDto) => {
    try {
        return await post<any>("/user/email/binding",bandingDto)
    } catch (error) {
        console.error("邮箱绑定出现问题: ", error);
    }
}

// 查询用户信息
interface UserInfo{
    id: number;
    openid: string;
    email: string;
    isPush: number;
  }
const userInfo = async()=>{
    try {
        return await get<UserInfo>("/user/info")
    } catch (error) {
        console.error("用户信息获取失败: ", error);
    }
}

// 修改推送状态
const updatePushStatus = async (isPush: number)=>{
    try {
        return await post<any>("/user/isPush/" + isPush)
    } catch (error) {
        console.error("推送设置失败: ", error);
    }
}
export {
    doLogin,
    getVertifyCode,
    emailBinding,
    userInfo,
    updatePushStatus
}